package me.croute.calendarexample.asynctask;

import java.io.IOException;
import java.util.ArrayList;

import me.croute.calendarexample.activity.GVCalendarSettingsActivity;

import android.util.Log;

import com.google.api.services.calendar.model.Event;
import com.google.api.services.calendar.model.Events;

public class GVCalendarHolidayAsyncTask extends GVCalendarAsyncTasks {
    private static final String TAG = "GVCalendarHolidayAsyncTask";
    private OnTaskCompleted onTaskListener;

    public GVCalendarHolidayAsyncTask(GVCalendarSettingsActivity activity) {
        super(activity);
        
    }

    private String CalendarID = null;
    private boolean isLoadSuccess = false;
    
    @Override
	protected void doInBackground() throws IOException {
		// TODO Auto-generated method stub
		Log.d(TAG, "doInBackground - CalendarID : " + CalendarID);
		if (CalendarID != null) { // except handling
			Events events = gvClient.events().list(CalendarID).execute();

			if (events != null) {
				GVCalendarSettingsActivity.holiday_events = events.getItems();

				for (Event event : events.getItems()) {
					Log.d(TAG, "event : " + event.getSummary());
				}
				isLoadSuccess = true;
			} else { // events is null, should be made exception.
				isLoadSuccess = false;
			}
			CalendarID = null; // reset, because AsyncTask has been survived.
		}

	}
    
    @Override
    protected void onPostExecute(Boolean result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        onTaskListener.onTaskCompleted(isLoadSuccess);
    }
    
    public static void run(GVCalendarSettingsActivity activity){
        GVCalendarHolidayAsyncTask task = new GVCalendarHolidayAsyncTask(activity);
        task.execute();
    }

    public static void run(GVCalendarSettingsActivity activity, String holiday_ID) {
        GVCalendarHolidayAsyncTask task = new GVCalendarHolidayAsyncTask(activity);
        task.CalendarID = holiday_ID;
        task.execute();
    }

    public static void run(GVCalendarSettingsActivity activity, String holiday_ID, OnTaskCompleted taskListener) {
        GVCalendarHolidayAsyncTask task = new GVCalendarHolidayAsyncTask(activity);
        task.onTaskListener = taskListener;
        task.CalendarID = holiday_ID;
        task.execute();
    }

}
